package com.huike.game.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.huike.game.R;
import com.huike.game.base.Game11;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Game12TemFragment extends Fragment {

    private Context context;
    private LineChart chart;
    List<Game11.DataBean> list;
    public Game12TemFragment(List<Game11.DataBean> list) {
        this.list = list;
    }
//    private Typeface mTf;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
//        mTf = Typeface.createFromAsset(context.getAssets(), "OpenSans-Regular.ttf");

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i< list.size()  ;i++){
            String time = list.get(i).getTime();
            String s = format.format(new Date(Long.valueOf(time)));
            Log.e("TAG",s);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view =  inflater.inflate(R.layout.game12tem, null);
                 chart =   view.findViewById(R.id.chart);

        chart.setDescription("not network");
        chart.setDrawGridBackground(false);

        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawAxisLine(true);
        // 横坐标文字
        //设置x轴间距


        YAxis leftAxis = chart.getAxisLeft();

        YAxis rightAxis = chart.getAxisRight();
        rightAxis.setEnabled(false);

        // set data
        chart.setData(generateDataLine(1));

        // do not forget to refresh the chart
        // chart.invalidate();
        chart.animateX(750);

        return view;
    }

    private LineData generateDataLine(int cnt) {

        ArrayList<Entry> e1 = new ArrayList<Entry>();
        int leng = list.size() <= 20 ? list.size() : 20;
        for (int i = 0; i < leng; i++) {
            e1.add(new Entry(list.get(i).getTem(), i));
        }

        LineDataSet d1 = new LineDataSet(e1, "°C");
        d1.setLineWidth(2.5f);
        d1.setCircleSize(4.5f);
        d1.setHighLightColor(Color.rgb(244, 117, 117));
        d1.setDrawValues(false);


        ArrayList<LineDataSet> sets = new ArrayList<LineDataSet>();
        sets.add(d1);
//        sets.add(d2);

        LineData cd = new LineData(getMonths(), sets);
        return cd;
    }

    @SuppressLint("SimpleDateFormat")
    private ArrayList<String> getMonths() {

        Log.e("TAG",list.size() +"");
        SimpleDateFormat format = new SimpleDateFormat("mm:ss");

        ArrayList<String> m = new ArrayList<String>();
        for (int i= 0; i < list.size(); i++){
            m.add(format.format(new Date(Long.valueOf(list.get(i).getTime()))));
        }

        return m;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
}
